package com.hutao.manage.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hutao.manage.pojo.BaseForSS;
import com.hutao.manage.pojo.BaseForTj;

public class CyTianshuDataUtil {
	
	public static CytianshuData getChongfuzhuYuanToMap_new(List<BaseForTj> sourceList) {
		Map<String, List<BaseForTj>> map = new HashMap<String, List<BaseForTj>>();
		List<BaseForTj> list = null;

		for (BaseForTj btj : sourceList) {
			String idcard = btj.getIdcard();
			list = map.get(idcard);
			if (list != null) {
				list.add(btj);
			} else {
				list = new ArrayList<BaseForTj>();
				list.add(btj);
				map.put(idcard, list);
			}
		}
		Iterator<Map.Entry<String, List<BaseForTj>>> it = map.entrySet()
				.iterator();
		CytianshuData rtData = new CytianshuData();
		int dangtianzzy = 0;// 当天在住院
		int shiwuzzy = 0;// 二到十五天在住院
		int erdaosanshiyizzy = 0;// 十六到三十一
		int sanshiyizzy = 0;// 三十一天以内
		int zzy2_7=0;
		int zzy8_14=0;
		int zzy15_31=0;
		while (it.hasNext()) {
			Map.Entry<String, List<BaseForTj>> entry = it.next();

			if (entry.getValue().size() <= 1) {
				it.remove();
			} else {
				List<BaseForTj> jlist = entry.getValue();
				Collections.sort(jlist, new Comparator<BaseForTj>() {
					public int compare(BaseForTj o1, BaseForTj o2) {
						
						return	o1.getCytime().compareTo(o2.getCytime());
						
					}
				});
				int listSize = jlist.size();
				for (int i = 0; i < listSize; i++) {
					// 上一次与下一次比较，用下面的减去上面的
					if (i == listSize - 1) {
						break;
					}
					BaseForTj hou = jlist.get(i + 1);
					Timestamp houruyuanTime = hou.getRytime();
					BaseForTj qian = jlist.get(i);
					Timestamp qiancyTime = qian.getCytime();
					String qianZyzd=qian.getZyzd();
					String houZyzd=qian.getZyzd();
					if(qianZyzd==houZyzd){
						int days = DateUtil.getXiangChaDays(
								houruyuanTime.getTime(), qiancyTime.getTime());
						if (days <= 1&&days >0) {
							dangtianzzy++;
						}
						if (days <= 7&&days >1) {
							zzy2_7++;
						}
						if (days <= 14&&days >7) {
							zzy8_14++;
						}
						if (days <= 31&&days >14) {
							zzy15_31++;
						}
						if (days >= 2 && days <= 15) {
							shiwuzzy++;
						}
						if (days >= 16 && days <= 31) {
							erdaosanshiyizzy++;
						}
						if (days <= 31&&days >0) {
							sanshiyizzy++;
							break;
						}
					}
				}
			}
		}
		rtData.setDangtianzzy(dangtianzzy);
		rtData.setShiwuzzy(shiwuzzy);
		rtData.setErdaosanshiyizzy(erdaosanshiyizzy);
		rtData.setSanshiyizzy(sanshiyizzy);
		rtData.setZzy2_7(zzy2_7);
		rtData.setZzy8_14(zzy8_14);
		rtData.setZzy15_31(zzy15_31);
		return rtData;
	}
	
	
	
	public static CytianshuData getSSToMap_new(List<BaseForSS> ssList) {
		Map<String, List<BaseForSS>> map = new HashMap<String, List<BaseForSS>>();
		List<BaseForSS> list = null;

		for (BaseForSS btj : ssList) {
			String banum = btj.getBanum();
			list = map.get(banum);
			if (list != null) {
				list.add(btj);
			} else {
				list = new ArrayList<BaseForSS>();
				list.add(btj);
				map.put(banum, list);
			}
		}
		Iterator<Map.Entry<String, List<BaseForSS>>> it = map.entrySet()
				.iterator();
		CytianshuData rtData = new CytianshuData();
		int ertianzzy = 0;// 当天在住院
		int sanshizzy = 0;// 30天在住院
		int sanshinei=0;

		while (it.hasNext()) {
			Map.Entry<String, List<BaseForSS>> entry = it.next();

			if (entry.getValue().size() <= 1) {
				it.remove();
			} else {
				List<BaseForSS> jlist = entry.getValue();
				Collections.sort(jlist, new Comparator<BaseForSS>() {
					public int compare(BaseForSS o1, BaseForSS o2) {
						
						return	o1.getOps_start().compareTo(o2.getOps_start());
						
					}
				});
				int listSize = jlist.size();
				for (int i = 0; i < listSize; i++) {
					// 上一次与下一次比较，用下面的减去上面的
					if (i == listSize - 1) {
						break;
					}
					BaseForSS hou = jlist.get(i + 1);
					String opsCode_hou=hou.getOpsCode();
					Timestamp houruyuanTime = hou.getOps_start();
					BaseForSS qian = jlist.get(i);
					String returnPlan=qian.getReturnPlan();
					String opsCode_qian=qian.getOpsCode();
					Timestamp qiancyTime = qian.getOps_end();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String qianTime = sdf.format(qiancyTime);
					String houTime = sdf.format(houruyuanTime);
					if(qianTime!=null){
						qianTime=qianTime.substring(0,13);
					}
					if(houTime!=null){
						houTime=houTime.substring(0,13);
					}
					
					if(!(qianTime.equals(houTime))&&!(qianTime.equals(houTime))&&!((opsCode_qian.contains("86.7")||opsCode_qian.contains("86.2")||opsCode_qian.contains("86.6")||opsCode_qian.contains("13.4100")||opsCode_qian.contains("38.5901")
							||opsCode_qian.contains("34.04")||opsCode_qian.contains("12.64")||opsCode_qian.contains("86.04")||opsCode_qian.contains("81.54"))
							&&(opsCode_hou.contains("86.7")||opsCode_hou.contains("86.2")||opsCode_hou.contains("86.6")||opsCode_qian.contains("13.4100")||opsCode_qian.contains("38.5901")
									||opsCode_hou.contains("34.04")||opsCode_hou.contains("12.64")||opsCode_hou.contains("86.04")||opsCode_hou.contains("81.54")	
									))&&(!(opsCode_qian.contains("79.3")&&opsCode_hou.contains("86.2")))&&(!(opsCode_qian.contains("14.79")&&opsCode_hou.contains("14.74")))
									&&(!(opsCode_qian.contains("25.02")&&opsCode_hou.contains("25.1")))){//&&(returnPlan==null||!"1".equals(returnPlan))
						
						
							int days = DateUtil.getXiangChaDays(
									houruyuanTime.getTime(), qiancyTime.getTime());
							if (days <= 2&&days >0) {
								ertianzzy++;
								System.out.println("前："+qian.getBanum()+",后："+hou.getBanum()+",前："+qian.getOps_end()+",后："+hou.getOps_start()+",前："+qian.getOpsCode()+",后："+hou.getOpsCode());

							}
							if (days > 2 && days <= 31) {
								sanshizzy++;
								System.out.println("前："+qian.getBanum()+",后："+hou.getBanum()+",前："+qian.getOps_end()+",后："+hou.getOps_start()+",前："+qian.getOpsCode()+",后："+hou.getOpsCode());
								if(sanshizzy==800){
									System.out.println("800");
								}
							
							}
							
							if (days <= 31&&days >0) {
								sanshinei++;
								break;
							}
					}	
				}
			}
		}
		rtData.setDangtianzzy(ertianzzy);
		rtData.setShiwuzzy(sanshizzy);
		rtData.setSanshiyizzy(sanshinei);
		return rtData;
	}


}
